package drds.common.db.xml_mapping.script.xml_tags;

import drds.common.db.xml_mapping.executor.parameter.ParameterHandler;
import drds.common.db.xml_mapping.mapping.BindSql;
import drds.common.db.xml_mapping.mapping.Mapper;
import drds.common.db.xml_mapping.mapping.SqlSource;
import drds.common.db.xml_mapping.parsing.PropertyParser;
import drds.common.db.xml_mapping.parsing.XmlNode;
import drds.common.db.xml_mapping.script.LanguageDriver;
import drds.common.db.xml_mapping.script.defaults.DefaultParameterHandler;
import drds.common.db.xml_mapping.script.defaults.RawSqlSource;
import drds.common.db.xml_mapping.session.Configuration;


public class XMLLanguageDriver implements LanguageDriver {

  @Override
  public ParameterHandler createParameterHandler(Mapper MappedStatement, Object parameterObject, BindSql bindSql) {
    return new DefaultParameterHandler(MappedStatement, parameterObject, bindSql);
  }

  @Override
  public SqlSource createSqlSource(Configuration configuration, XmlNode script, Class<?> parameterType) {
    XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
    return builder.parseScriptNode();
  }

  @Override
  public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {

    {

      script = PropertyParser.parse(script, configuration.getVariables());
      Text textSqlNode = new Text(script);
      if (textSqlNode.isDynamic()) {
        return new DynamicSqlSource(configuration, textSqlNode);
      } else {
        return new RawSqlSource(configuration, script, parameterType);
      }
    }
  }

}
